package de.szut.traineeslife.db;

import java.util.ArrayList;
import java.util.List;

import de.szut.traineeslife.data.Report;
import de.szut.traineeslife.data.User;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseHandler extends SQLiteOpenHelper {

	private static final int DATABASE_VERSION = 1;

	private static final String DATABASE_NAME = "DatenListe";

	public interface Tables {
		// Contacts table name
		static final String TABLE_REPORTS = "Reports";
	}

	// Contacts Table Columns names

	public DataBaseHandler(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String CREATE_TABLE_USERS = "CREATE TABLE " + Tables.TABLE_REPORTS + "(" + de.szut.traineeslife.db.ReportContract.Report.KEY_ID + " INTEGER PRIMARY KEY, "
				+ de.szut.traineeslife.db.ReportContract.Report.KEY_TITEL + " TEXT," + de.szut.traineeslife.db.ReportContract.Report.KEY_TEXT + " TEXT,"
				+ de.szut.traineeslife.db.ReportContract.Report.KEY_STARTDATE + " TEXT," + de.szut.traineeslife.db.ReportContract.Report.KEY_ENDDATE + " TEXT"
				+ de.szut.traineeslife.db.ReportContract.Report.KEY_TEXT_SQL + " TEXT" + de.szut.traineeslife.db.ReportContract.Report.KEY_MODEL_PATH + " TEXT"
				+ de.szut.traineeslife.db.ReportContract.Report.KEY_HOURS + " TEXT" + ")";
		db.execSQL(CREATE_TABLE_USERS);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + Tables.TABLE_REPORTS);
		onCreate(db);
	}

	public void addReport(Report report) {
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(de.szut.traineeslife.db.ReportContract.Report.KEY_ID, report.getId());
		values.put(de.szut.traineeslife.db.ReportContract.Report.KEY_TITEL, report.getTitel());
		values.put(de.szut.traineeslife.db.ReportContract.Report.KEY_STARTDATE, report.getStartDate());
		values.put(de.szut.traineeslife.db.ReportContract.Report.KEY_ENDDATE, report.getEndDate());
		values.put(de.szut.traineeslife.db.ReportContract.Report.KEY_TEXT, report.getReportText());
		values.put(de.szut.traineeslife.db.ReportContract.Report.KEY_TEXT_SQL, report.getReportTextSchool());
		values.put(de.szut.traineeslife.db.ReportContract.Report.KEY_HOURS, report.getHoursOfWork());
		values.put(de.szut.traineeslife.db.ReportContract.Report.KEY_MODEL_PATH, report.getPathOfModel());

		db.insert(Tables.TABLE_REPORTS, null, values);
		db.close();
	}

	public Report getReport(int id) {
		SQLiteDatabase db = getWritableDatabase();
		Cursor cursor = db.query(Tables.TABLE_REPORTS, new String[] { de.szut.traineeslife.db.ReportContract.Report.KEY_ID, de.szut.traineeslife.db.ReportContract.Report.KEY_TITEL,
				de.szut.traineeslife.db.ReportContract.Report.KEY_STARTDATE, de.szut.traineeslife.db.ReportContract.Report.KEY_ENDDATE, de.szut.traineeslife.db.ReportContract.Report.KEY_TEXT,
				de.szut.traineeslife.db.ReportContract.Report.KEY_TEXT_SQL, de.szut.traineeslife.db.ReportContract.Report.KEY_HOURS, de.szut.traineeslife.db.ReportContract.Report.KEY_MODEL_PATH, },
				de.szut.traineeslife.db.ReportContract.Report.KEY_ID + "=?", new String[] { String.valueOf(id) }, null, null, null, null);
		if (cursor != null) {
			cursor.moveToFirst();
		}
		Report report = new Report(Integer.parseInt(cursor.getString(cursor.getColumnIndex(de.szut.traineeslife.db.ReportContract.Report.KEY_ID))), cursor.getString(cursor
				.getColumnIndex(de.szut.traineeslife.db.ReportContract.Report.KEY_TITEL)), cursor.getString(cursor.getColumnIndex(de.szut.traineeslife.db.ReportContract.Report.KEY_STARTDATE)),
				cursor.getString(cursor.getColumnIndex(de.szut.traineeslife.db.ReportContract.Report.KEY_ENDDATE)), cursor.getString(cursor
						.getColumnIndex(de.szut.traineeslife.db.ReportContract.Report.KEY_TEXT)), cursor.getString(cursor.getColumnIndex(de.szut.traineeslife.db.ReportContract.Report.KEY_TEXT_SQL)),
				cursor.getString(cursor.getColumnIndex(de.szut.traineeslife.db.ReportContract.Report.KEY_HOURS)), cursor.getString(cursor
						.getColumnIndex(de.szut.traineeslife.db.ReportContract.Report.KEY_MODEL_PATH)));
		return report;
	}

	public List<Report> getAllReports() {
		List<Report> reportsList = new ArrayList<Report>();

		String selectQuery = "SELECT  * FROM " + Tables.TABLE_REPORTS;

		SQLiteDatabase db = getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);

		if (cursor.moveToFirst()) {
			do {
				Report report = new Report();
				report.setId(Integer.parseInt(cursor.getString(cursor.getColumnIndex(de.szut.traineeslife.db.ReportContract.Report.KEY_ID))));
				report.setTitel(cursor.getString(cursor.getColumnIndex(de.szut.traineeslife.db.ReportContract.Report.KEY_TITEL)));
				report.setStartDate(cursor.getString(cursor.getColumnIndex(de.szut.traineeslife.db.ReportContract.Report.KEY_STARTDATE)));
				report.setEndDate(cursor.getString(cursor.getColumnIndex(de.szut.traineeslife.db.ReportContract.Report.KEY_ENDDATE)));
				report.setReportText(cursor.getString(cursor.getColumnIndex(de.szut.traineeslife.db.ReportContract.Report.KEY_TEXT)));
				report.setReportTextSchool(cursor.getString(cursor.getColumnIndex(de.szut.traineeslife.db.ReportContract.Report.KEY_TEXT_SQL)));
				report.setHoursOfWork(cursor.getString(cursor.getColumnIndex(de.szut.traineeslife.db.ReportContract.Report.KEY_HOURS)));
				report.setPathOfModel(cursor.getString(cursor.getColumnIndex(de.szut.traineeslife.db.ReportContract.Report.KEY_MODEL_PATH)));
				reportsList.add(report);

			} while (cursor.moveToNext());
		}
		return reportsList;
	}

	public int getUsersCount() {
		String countQuery = "SELECT * FROM " + Tables.TABLE_REPORTS;
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(countQuery, null);
		cursor.close();
		return cursor.getCount();
	}

	public int updateArea(Report report) {
		SQLiteDatabase db = getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(de.szut.traineeslife.db.ReportContract.Report.KEY_ID, report.getId());
		values.put(de.szut.traineeslife.db.ReportContract.Report.KEY_TITEL, report.getTitel());
		values.put(de.szut.traineeslife.db.ReportContract.Report.KEY_STARTDATE, report.getStartDate());
		values.put(de.szut.traineeslife.db.ReportContract.Report.KEY_ENDDATE, report.getEndDate());
		values.put(de.szut.traineeslife.db.ReportContract.Report.KEY_TEXT, report.getReportText());
		values.put(de.szut.traineeslife.db.ReportContract.Report.KEY_TEXT_SQL, report.getReportTextSchool());
		values.put(de.szut.traineeslife.db.ReportContract.Report.KEY_HOURS, report.getHoursOfWork());
		values.put(de.szut.traineeslife.db.ReportContract.Report.KEY_MODEL_PATH, report.getPathOfModel());

		return db.update(Tables.TABLE_REPORTS, values, de.szut.traineeslife.db.ReportContract.Report.KEY_ID + "= ?", new String[] { String.valueOf(report.getId()) });
	}

	// Deleting single Area
	public void deleteArea(User user) {

	}

}
